<?php

namespace Moonpie\Plugin\Enhancer\Service\Doctrine;

use Doctrine\ORM\ORMException;
use Doctrine\Persistence\AbstractManagerRegistry;
use Psr\Container\ContainerInterface;

class ManagerRegistry extends AbstractManagerRegistry
{
    protected $container;

    public function __construct(ContainerInterface $container, $name, array $connections, array $managers, $defaultConnection, $defaultManager)
    {
        $this->container = $container;
        parent::__construct($name, $connections, $managers, $defaultConnection, $defaultManager, 'Doctrine\ORM\Proxy\Proxy');
    }

    protected function getService($name)
    {
        return $this->container->get($name);
    }

    protected function resetService($name)
    {
        if ($this->container->has($name)) {
            unset($this->container[$name]);
        }
    }

    public function getAliasNamespace($alias)
    {
        foreach (array_keys($this->getManagers()) as $name) {
            try {
                return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
            } catch (ORMException $e) {
            }
        }

        throw ORMException::unknownEntityNamespace($alias);
    }
}
